package com.johnny.cms.core.controller.nob;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.johnny.cms.core.domain.Conf;
import com.johnny.cms.core.domain.resource.WebResource;
import com.johnny.cms.core.service.CmsContentService;
import com.johnny.cms.core.service.CmsNOBService;
import com.johnny.cms.core.service.CmsSystemService;
import com.johnny.core.web.JsonMessage;
import com.johnny.core.web.Webs;
import com.johnny.core.web.Webs.ContentType;
import com.johnny.core.web.controller.ControllerSupport;
import com.johnny.core.web.exception.BadRequestException;
import com.johnny.lang.Lang;
import com.johnny.nob.domain.NboContact;

@Controller(value = "nbodownloadC")
@RequestMapping("/nbodownload")
public class NobDownloadController extends ControllerSupport {
	@Autowired
	private CmsSystemService cmsSystemService;
	@Autowired
	private CmsContentService cmsContentService;
	@Autowired
	private CmsNOBService cmsNOBService;

	@RequestMapping(value = "/submit/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonMessage submit(@Valid NboContact nobContact,
			BindingResult result, HttpServletRequest httpServletRequest) {

		Subject subject = SecurityUtils.getSubject();

		if (subject.isAuthenticated()) {
			return super.onSuccess();
		}
		HttpSession session = httpServletRequest.getSession(true);
		if (session.getAttribute("submit_form") != null) {
			return super.onSuccess();
		}

		if (!result.hasErrors()) {
			cmsNOBService.createNobContact(nobContact);
			session.setAttribute("submit_form", Boolean.TRUE);
			return super.onSuccess();
		} else {
			return super.onFailure(new RuntimeException("wrong"));
		}

	}

	@RequestMapping(value = "/download/", method = RequestMethod.GET)
	public void download(@RequestParam(value = "path") String pathname,
			HttpServletResponse response, HttpServletRequest httpServletRequest) {

		HttpSession session = httpServletRequest.getSession(true);
		if (session.getAttribute("submit_form") == null
				&& !SecurityUtils.getSubject().isAuthenticated()) {
			throw new BadRequestException();
		}

		Conf conf = cmsSystemService.getWrapConf();
		WebResource bean = cmsContentService.getResourceObject(conf,
				createDefalutDirectoryIfNeccessary(pathname));

		if (null == bean || !bean.isReadable()) {
			throw new BadRequestException();
		}

		try {

			Webs.prepareDownload(response, bean.getName(), ContentType.OCTET);
			if (bean.isDirectory()) {
				throw new BadRequestException();
			}

			bean.selfAdjusting(conf).piping(response.getOutputStream());
		} catch (Exception e) {
			throw Lang.uncheck(e);
		}

	}

	private String createDefalutDirectoryIfNeccessary(String pathname) {
		return StringUtils.isBlank(pathname) ? "/" : pathname;
	}
}
